let json2xml = require('pt-core-common').util.json2xml;
let feedsTemplate = require('./feeds.templates.func');

const imageTypeDict = {"M": "Main", "D": "BKLB", "O": "OfferImage1"};
const imageServerBasePath = "http://localhost:3000/api/admproducts/factimage/";

let feedsProductImageTemplate = {
    messageListTemplate: function(operationType, sku, productImageList){
        let messageList = [];
        let index = 0;
        productImageList.forEach(productImage => {
            index++;
            messageList.push({
                MessageID: index,
                OperationType: operationType,
                ProductImage: {
                    SKU: sku,
                    ImageType: imageTypeDict[productImage.imageType],
                    ImageLocation:  imageServerBasePath + productImage.imageName
                }
            })
        });
        return messageList;
    },
    amazonEnvelopeTemplate2xml: function(sellerId, operationType, sku, productImageList){
        const messageList = feedsProductImageTemplate.messageListTemplate(operationType, sku, productImageList);
        return json2xml("AmazonEnvelope", feedsTemplate.amazonEnvelopeTemplate(sellerId, "ProductImage", messageList, {PurgeAndReplace: "false"}));
    }
};

module.exports = feedsProductImageTemplate;